Skip to content

CMake Code Snippets ​

  • Show all options
bash
cmake -LA

PRIVATE / PUBLIC / INTERFACE ​

Ref: Transitive Usage Requirements

Keyword自己用傳給依賴自己的 target什麼時候用
PRIVATE✅❌只有 cpp 用到
INTERFACE❌✅只有 header 用到
PUBLIC✅✅cpp 和 header 都用到

Libraries and targets following PUBLIC are linked to, and are made part of the link interface. Libraries and targets following PRIVATE are linked to, but are not made part of the link interface. Libraries following INTERFACE are appended to the link interface and are not used for linking <target>. -- target_link_libraries

範例:main.c 依賴 hello_world.so,hello_world.so 依賴 hello。

專案依賴:main.c → hello_world.so → hello.so

PRIVATE:只有 hello_world.so 看得到 hello。

PRIVATE:hello_world.so 依賴 hello.so 和 hello.hpp,main.c 只依賴 hello_world.so

INTERFACE:hello 傳給 main.c,hello_world.so 本身不 link。

INTERFACE:main.c 依賴 hello_world.so、hello.so、hello.hpp

PUBLIC:兩邊都看得到。

PUBLIC:hello_world.so 和 main.c 都依賴 hello.so 和 hello.hpp

用 PROTOBUF_LIBRARIES(複數),不是 PROTOBUF_LIBRARY:後者不含 pthread 等依賴,會在 runtime 噴 libprotobuf FATAL ... SCCInfoBase::kRunning。

cmake
find_package(Protobuf 3.14.0 REQUIRED)
target_link_libraries(main proto ${PROTOBUF_LIBRARIES})
# 或用 imported target:protobuf::libprotobuf

原文:Protobuf 3.14.0 和 CMake 的一些小細節

頁面歷史

Just observe 👀